<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/1/16
 * Time: 11:23
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Thrule extends Base
{
    /**
     * 特惠礼包权限列表
     * @return mixed|\think\response\Json
     */
    public function thRule(){
        if(request()->isAjax ()){
            $where['rule_status'] = 1;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('th_rule')->where($where)->count();// 获取总条数
            $lists = Db::name('th_rule')
                ->where($where)
                ->field('rule_id, rule_name, rule_update_time')
                ->order('rule_id asc')
                ->page($Nowpage,$limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 编辑权限
     * @return mixed|\think\response\Json
     */
    public function ruleEdit(){
        $rule_id = input('rule_id');
        if(Request::instance()->isPost()){
            $rule_name = trim(input('rule_name'));
            $res = Db::table('up_th_rule')
                ->update([
                    'rule_id' => $rule_id,
                    'rule_name' => $rule_name,
                    'rule_update_time' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $rule = Db::table('up_th_rule')->where('rule_id', $rule_id)->field('rule_name')->find();
        $this->assign([
            'rule_id' => $rule_id,
            'rule' => $rule
        ]);
        return $this->fetch();
    }
}